| Conditions | 2 |
| Paths | 2 |
| Total Lines | 79 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** findMdpInsert and findCode functions use a similar layout to return the location and contents |
||
| 54 | function _findFencedCode (txt, start) { |
||
| 55 | // if the internalLength is returned as -1 this means that text cannot simply be inserted at the internalStart |
||
| 56 | // location. Instead an additional preceding new line must be inserted along with the new text |
||
| 57 | // another way to look at this is that the internal text is 1 character short |
||
| 58 | // a value of -2 indicates a CRLF needs to be inserted |
||
| 59 | let a = _findOpeningCodeFence(txt, start) |
||
| 60 | if (a.start === -1) { return a } |
||
| 61 | return _findClosingCodeFence(txt, a) |
||
| 62 | |||
| 63 | function _findOpeningCodeFence (txt, start) { |
||
| 64 | // returns the location and type of the next opening code fence |
||
| 65 | let regex = /(^|\r\n|\n)([ ]{0,3}> |>|[ ]{0,0})(([ ]{0,3})([`]{3,}|[~]{3,})([^\n\r\0`]*))($|\r\n|\n)/g |
||
| 66 | /** The regex groups are: |
||
| 67 | * 0: the full match including any preamble block markup |
||
| 68 | * 1: the leading new line character(s) |
||
| 69 | * 2: the preamble consisting of block characters or nothing |
||
| 70 | * 3: the full codeFence line without preamble |
||
| 71 | * 4: any leading blank spaces at the start of the codeFence line |
||
| 72 | * 5: the ` or ~ characters identifying the codeFence |
||
| 73 | * 6: anything else on the line following the codeFence |
||
| 74 | * 7: the final new line character(s) |
||
| 75 | **/ |
||
| 76 | regex.lastIndex = start |
||
| 77 | let regexResult = regex.exec(txt) |
||
| 78 | if (regexResult === null) { |
||
| 79 | return {start: -1} |
||
| 80 | } |
||
| 81 | let r = { start: regexResult.index + regexResult[1].length, |
||
| 82 | info: { |
||
| 83 | blockQuote: regexResult[2], |
||
| 84 | spacesCount: regexResult[4].length, |
||
| 85 | codeFence: regexResult[5] |
||
| 86 | }, |
||
| 87 | commandString: regexResult[6].trim(), |
||
| 88 | internalStart: regexResult.index + regexResult[0].length |
||
| 89 | } |
||
| 90 | return r |
||
| 91 | } |
||
| 92 | |||
| 93 | function _findClosingCodeFence (txt, opening) { |
||
| 94 | // updates the passed result structure with the location and type of the next closing code fence |
||
| 95 | // to match the opening cofeFence passed in |
||
| 96 | let regex |
||
| 97 | let r = JSON.parse(JSON.stringify(opening)) // create copy of opening structure passed in |
||
| 98 | regex = RegExp('(^|\r\n|\n)([ ]{0,3}> |>|[ ]{0,0})[ ]{0,3}[' + r.info.codeFence[0] + ']{' + r.info.codeFence.length + ',}[ ]*($|\r\n|\n)', 'g') |
||
| 99 | regex.lastIndex = r.internalStart - 2 |
||
| 100 | let regexResult = regex.exec(txt) |
||
| 101 | if (opening.info.blockQuote.length !== 0) { |
||
| 102 | // we are in a block quote so the codeFence will end at the earlier of the found regex OR end of the block quote |
||
| 103 | let b = _findEndOfBlock(txt, r.internalStart) |
||
| 104 | if (b !== -1 && (regexResult === null || b < (regexResult.index + regexResult[1].length))) { |
||
| 105 | // the block end dictates the code block end |
||
| 106 | r.internalLength = b - r.internalStart |
||
| 107 | r.length = b - r.start |
||
| 108 | return r |
||
| 109 | } |
||
| 110 | } |
||
| 111 | if (regexResult === null) { |
||
| 112 | r.internalLength = txt.length - r.internalStart |
||
| 113 | r.length = txt.length - r.start |
||
| 114 | } else { |
||
| 115 | r.internalLength = regexResult.index - r.internalStart |
||
| 116 | r.length = regexResult.index + regexResult[0].length - regexResult[3].length - r.start |
||
| 117 | } |
||
| 118 | return r |
||
| 119 | } |
||
| 120 | |||
| 121 | function _findEndOfBlock (txt, start) { |
||
| 122 | // finds the first line which is not marked as block |
||
| 123 | let regex = /(\r\n|\n)(?!([ ]{0,3}> |>))[^>\r\n]*/g |
||
| 124 | regex.lastIndex = start |
||
| 125 | let regexResult = regex.exec(txt) |
||
| 126 | if (regexResult === null) { |
||
| 127 | return -1 |
||
| 128 | } else { |
||
| 129 | return regexResult.index |
||
| 130 | } |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 194 |